<?php

namespace App\Http\Controllers;

use App\Models\Staffs;
use App\Models\Tokens;
use App\Models\Users;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use PhpParser\Token;

class AuthController extends Controller
{
    private function createToken($role , $id){
        $token = time() + rand(100000 , 999999);

        Tokens::create([
            'object_id' => $id,
            'token' => $token,
            'role' => '0'
        ]);

        return $token;
    }

    public function userRegister(){
        $data = request()->only(['email' , 'password' , 'repeat_password' , 'phone_number' , 'first_name' , 'last_name']);
        $validation = Validator::make($data , [
            'email' => 'required',
            'password' => 'required | min:4',
            'repeat_password' => 'required | min:4',
            'phone_number' => 'required | between:8,12',
            'first_name' => 'required',
            'last_name' => 'required'
        ]);
        if($validation->fails()){
            return $this->returnValidationError('data can not be processed');
        }

        if ($data['password'] != $data['repeat_password']) {
            return $this->returnValidationError('repeat_password is not same with password field');
        }

        if (Users::where('email' , $data['email'])->exists()) {
            return $this->returnValidationError('email has already been used');
        }

        $user = Users::create([
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'phone' => $data['phone_number'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name']
        ]);

        $token = $this->createToken(0 , $user->id);

        return $this->returnJson([
            'id' => $user->id,
            'email' => $user->email,
            'phone' => $user->phone_number,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'token' => $token
        ]);
    }

    public function userLogin(){
        $data = request()->only(['email' , 'password']);
        $validation = Validator::make($data , [
            'email' => 'required | email',
            'password' => 'required | min:4'
        ]);
        if($validation->fails()){
            return $this->returnValidationError('data can not be processed');
        }

        $user = Users::where("email" , $data['email'])->first();

        if ($user == null || !Hash::check($data['password'] , $user->password)) {
            return $this->returnUnauthenticatedError('client credentials are invalid');
        }
        $token = $this->createToken(0 , $user->id);

        return $this->returnJson([
            'id' => $user->id,
            'email' => $user->email,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'token' => $token
        ]);
    }

    public function userLogout(){
        $token = request()->only('token');
        Tokens::where("token" , $token)->delete();
        return $this->returnJson();
    }

    public function staffLogin(){
        $data = request()->only(['email' , 'password']);
        $validation = Validator::make($data , [
            'email' => 'required | email',
            'password' => 'required | min:4'
        ]);
        if($validation->fails()){
            return $this->returnValidationError('data can not be processed');
        }
        $staff = Staffs::where("email" , $data['email'])->first();

        if ($staff == null || !Hash::check($data['password'] , $staff->password)) {
            return $this->returnUnauthenticatedError("staff credentials are invalid");
        }
        $token = $this->createToken($staff->position == 0 ? 2 : 3 , $staff->id);
        return $this->returnJson([
            'id' => $staff->id,
            'email' => $staff->email,
            'first_name' => $staff->first_name,
            'last_name' => $staff->last_name,
            'token' => $token,
            'role' => $staff->position == 0 ? 'courier' : 'trucker'
        ]);
    }

    public function staffLogout(){
        $token = request()->only('token');
        Tokens::where("token" , $token)->delete();
        return $this->returnJson();
    }
}
